home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / debuging < prev    next >
Text File  |  1996-01-05  |  4KB  |  166 lines

  1. #ifdef DEBUG
  2. /*----------------------------------------------------------------------
  3.      Initialize debugging - open the debug log file
  4.  
  5.   Args: none
  6.  
  7.  Result: opens the debug logfile for dprints
  8.  
  9.    Opens the file "~/.pine-debug1. Also maintains .pine-debug[2-4]
  10.    by renaming them each time so the last 4 sessions are saved.
  11.   ----*/
  12. void
  13. init_debug()
  14. {
  15.     char nbuf[5];
  16.     char newfname[MAXPATH+1], filename[MAXPATH+1];
  17.     int i;
  18.  
  19.     if(!debug)
  20.       return;
  21.  
  22.     for(i = NUMDEBUGFILES - 1; i > 0; i--){
  23.         build_path(filename, ps_global->home_dir, DEBUGFILE);
  24.         strcpy(newfname, filename);
  25.         sprintf(nbuf, "%d", i);
  26.         strcat(filename, nbuf);
  27.         sprintf(nbuf, "%d", i+1);
  28.         strcat(newfname, nbuf);
  29.         (void)rename_file(filename, newfname);
  30.     }
  31.  
  32.     build_path(filename, ps_global->home_dir, DEBUGFILE);
  33.     strcat(filename, "1");
  34.  
  35.     debugfile = fopen(filename, "w+");
  36.     if(debugfile != NULL){
  37.     time_t now = time((time_t *)0);
  38.     if(debug > 7)
  39.       setbuf(debugfile, NULL);
  40.  
  41.     if(NUMDEBUGFILES == 0){
  42.         /*
  43.          * If no debug files are asked for, make filename a tempfile
  44.          * to be used for a record should pine later crash...
  45.          */
  46.         if(debug < 9)
  47.           unlink(filename);
  48.     }
  49.  
  50.     dprint(1, (debugfile, "Debug output of the Pine program (at debug"));
  51.     dprint(1, (debugfile, " level %d).  Version %s\n%s\n",
  52.           debug, pine_version, ctime(&now)));
  53.     }
  54. }
  55.  
  56.  
  57. /*----------------------------------------------------------------------
  58.      Try to save the debug file if we crash in a controlled way
  59.  
  60.   Args: dfile:  pointer to open debug file
  61.  
  62.  Result: tries to move the appropriate .pine-debugx file to .pine-crash
  63.  
  64.    Looks through the four .pine-debug files hunting for the one that is
  65.    associated with this pine, and then renames it.
  66.   ----*/
  67. void
  68. save_debug_on_crash(dfile)
  69. FILE *dfile;
  70. {
  71.     char nbuf[5], crashfile[MAXPATH+1], filename[MAXPATH+1];
  72.     int i;
  73.     struct stat dbuf, tbuf;
  74.     time_t now = time((time_t *)0);
  75.  
  76.     if(!(dfile && fstat(fileno(dfile), &dbuf) != 0))
  77.       return;
  78.  
  79.     fprintf(dfile, "\nsave_debug_on_crash: Version %s: debug level %d\n",
  80.     pine_version, debug);
  81.     fprintf(dfile, "\n                   : %s\n", ctime(&now));
  82.  
  83.     build_path(crashfile, ps_global->home_dir, ".pine-crash");
  84.  
  85.     fprintf(dfile, "\nAttempting to save debug file to %s\n", crashfile);
  86.     fprintf(stderr,
  87.     "\n\n       Attempting to save debug file to %s\n\n", crashfile);
  88.  
  89.     /* Blat out last n keystrokes */
  90.     fputs("========== Latest keystrokes ==========\n", dfile);
  91.     while((i = key_recorder(0, 1)) != -1)
  92.       fprintf(dfile, "\t%s\t(0x%04.4x)\n", pretty_command(i), i);
  93.  
  94.     /* look for existing debug file */
  95.     for(i = 1; i <= NUMDEBUGFILES; i++){
  96.     build_path(filename, ps_global->home_dir, DEBUGFILE);
  97.     sprintf(nbuf, "%d", i);
  98.     strcat(filename, nbuf);
  99.     if(stat(filename, &tbuf) != 0)
  100.       continue;
  101.  
  102.     /* This must be the current debug file */
  103.     if(tbuf.st_dev == dbuf.st_dev && tbuf.st_ino == dbuf.st_ino){
  104.         rename_file(filename, crashfile);
  105.         break;
  106.     }
  107.     }
  108.  
  109.     /* if current debug file name not found, write it by hand */
  110.     if(i > NUMDEBUGFILES){
  111.     FILE *cfp;
  112.     char  buf[1025];
  113.  
  114.     /*
  115.      * Copy the debug temp file into the 
  116.      */
  117.     if(cfp = fopen(crashfile, "w")){
  118.         buf[1024] = '\0';
  119.         fseek(dfile, 0L, 0);
  120.         while(fgets(buf, 1025, dfile) && fputs(buf, cfp) != EOF)
  121.           ;
  122.  
  123.         fclose(cfp);
  124.     }
  125.     }
  126.  
  127.     fclose(dfile);
  128. }
  129.  
  130.  
  131. #define CHECK_EVERY_N_TIMES 100
  132. #define MAX_DEBUG_FILE_SIZE 200000L
  133. /*
  134.  * This is just to catch runaway Pines that are looping spewing out
  135.  * debugging (and filling up a file system).  The stop doesn't have to be
  136.  * at all precise, just soon enough to hopefully prevent filling the
  137.  * file system.  If the debugging level is high (9 for now), then we're
  138.  * presumably looking for some problem, so don't truncate.
  139.  */
  140. int
  141. do_debug(debug_fp)
  142. FILE *debug_fp;
  143. {
  144.     static int counter = CHECK_EVERY_N_TIMES;
  145.     static int ok = 1;
  146.     long filesize;
  147.  
  148.     if(debug == DEFAULT_DEBUG && ok && --counter <= 0){
  149.     if((filesize = fp_file_size(debug_fp)) != -1L)
  150.       ok = (unsigned long)filesize < (unsigned long)MAX_DEBUG_FILE_SIZE;
  151.  
  152.     counter = CHECK_EVERY_N_TIMES;
  153.     if(!ok){
  154.         fprintf(debug_fp, "\n\n --- No more debugging ---\n");
  155.         fprintf(debug_fp,
  156.         "     (debug file growing too large - over %ld bytes)\n\n",
  157.         MAX_DEBUG_FILE_SIZE);
  158.         fflush(debug_fp);
  159.     }
  160.     }
  161.     return(ok);
  162. }
  163. #endif /* DEBUG */
  164.  
  165.  
  166.